﻿<?php
	class Resources{
	
		private $name;     //指标名,初始化时必须赋值
		private $zone;     //地区ID
		private $value;    //指标值
		private $unit;     //指标单位
		private $fromDate;
		private $toDate;
		private $selectQuery;
		private $conn;
		
		private $economy = array("0"=>"人均GDP增长率","1"=>"GDP三产比重增长率","2"=>"城乡收入差距","3"=>"城镇居民人均住房使用面积");
		private $society = array("0"=>"中等教育人数比重","1"=>"净迁移率","2"=>"千人拥有病床数","3"=>"人均建成用地面积");
		private $ecology = array("0"=>"非建成用地比重变化率","1"=>"水环境综合污染指数","2"=>"万元GDP综合能耗变化率","3"=>"地均GDP");
		private $rangeZone = array("0"=>'811',"1"=>'823');

		
		function __construct(){
			$this->conn = conn();
			$this->setUnit();

		}	
		
		public function setName($name){
			$this->name = $name;
		}
		
		public function setZone($zone){
			$this->zone = $zone;
		}
		
		public function setFromDate($fromDate){
			$this->fromDate = $fromDate;
		}
		
		public function setToDate($toDate){
			$this->toDate = $toDate;
		}
			
		private function createOptionQuery(){
			$this->selectQuery = "";
			$this->selectQuery .= "where Name = '".$this->name."' ";
			if(!empty($this->fromDate))
				$this->selectQuery .= " and FromDate >= '".$this->fromDate."' ";
			if(!empty($this->toDate))
				$this->selectQuery .= " and ToDate <= '".$this->toDate."' ";
			if(!empty($this->zone))
				$this->selectQuery .= " and Zone = '".$this->zone."' ";

				}
		
		//输出当前情况下所对应Zone情况的列表
		public function listZone(){
			$this->createOptionQuery();
			$this->selectQuery = "select distinct Zone from I_INDEX ".$this->selectQuery;
			$query = $this->selectQuery;
			$stmt = $this->conn->query( $query ); 
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				print_r( "Zone: ".$row['Zone']."<br/>");			
			}	
		}
		
		//输出当前情况下所对应FromDate情况的列表
		public function listFromDate(){
			$this->createOptionQuery();
			$this->selectQuery = "select distinct FromDate from I_INDEX ".$this->selectQuery;
			$query = $this->selectQuery;
			$stmt = $this->conn->query( $query ); 
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				print_r( "Zone: ".$row['FromDate']."<br/>");			
			}	
		}
		
		//输出当前情况下所对应ToDate情况的列表
		public function listToDate(){
			$this->createOptionQuery();
			$this->selectQuery = "select distinct ToDate from I_INDEX ".$this->selectQuery;
			$query = $this->selectQuery;
			$stmt = $this->conn->query( $query ); 
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				print_r( "Zone: ".$row['ToDate']."<br/>");			
			}	
		}
		
		//输出数据库中所有Name列表
		public function listAllNames(){
			$query = "select distinct Name from I_INDEX";
			$stmt = $this->conn->query( $query );
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) )
				echo $row['Name']." ; ";
		}
			
		private function setUnit(){
			$query = "select * from I_INDEX where Name = '".$this->name."'";
			$stmt = $this->conn->query( $query ); 
			$row = $stmt->fetch( PDO::FETCH_ASSOC );
			$this->unit = ($row['Unit']);
		}
		//获取指标单位
		public function getUnit(){
			return $tihs->unit;
		}
		
		public function getYear($date){
			 $str = explode('-',$date);			 
			 return $str[0];
		}
		
		public function getFromDateByYear($year){
			$date = $year."-01-01";
			return $date;
		}		
		
		public function getToDateByYear($year){
			$date = $year."-12-31";
			return $date;
		}
			
		public function getZoneDetail($zone){
			$query = "select * from I_ZONE where ID = '".$zone."'";
			//echo $query;
			$stmt = $this->conn->query( $query ); 
			$row = $stmt->fetch( PDO::FETCH_ASSOC );
			$province = $row['Province'];
			$district = $row['District'];
			return $district;
		}
		
		public function getZoneByDistrict($district){
			$query = "select * from I_ZONE where District = '".$district."'";
			//echo $query;
			$stmt = $this->conn->query( $query ); 
			$row = $stmt->fetch( PDO::FETCH_ASSOC );
			$id = $row['ID'];
			//echo $id;
			return $id;
		}
			
		public function listAll(){

			$query = "select * from I_INDEX where ( Name = '".$this->economy[0]."' or Name = '".$this->economy[1]."' or Name = '".$this->economy[2]."' or Name = '".$this->economy[3]."') and Zone >='".$this->rangeZone[0]."' and Zone <= '".$this->rangeZone[1]."'";
			//echo $query;
			$stmt = $this->conn->query( $query ); 
			$items = array();
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				$id = $row['ID'];
				$name = $row['Name'];
				$zone = $row['Zone'];
				$fromDate = $row['FromDate'];
				//cut date to year
				$fromDate = $this->getYear($fromDate);
				$value = $row['Value'];
				//getDetailZone
				$zone = $this->getZoneDetail($zone);
				//print_r( "Name: ".$name.", Zone: ".$zone.", ID: ".$id.", From: ".$fromDate."<br/>");	
				$item=array("id"=>$id,"area"=>$zone,"time"=>$fromDate,"indicator"=>$name,"value"=>$value,"p5"=>$this->unit);
				array_push($items, $item);
							
			}				
			//print_r($items);
			return $items;
		}


		public function listAllEcology(){
			$query = "select * from I_INDEX where ( Name = '".$this->ecology[0]."' or Name = '".$this->ecology[1]."' or Name = '".$this->ecology[2]."' or Name = '".$this->ecology[3]."') and Zone >='".$this->rangeZone[0]."' and Zone <= '".$this->rangeZone[1]."'";
			$stmt = $this->conn->query( $query ); 
			$items = array();
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				$id = $row['ID'];
				$name = $row['Name'];
				$zone = $row['Zone'];
				$fromDate = $row['FromDate'];
				//cut date to year
				$fromDate = $this->getYear($fromDate);
				$value = $row['Value'];
				//getDetailZone
				$zone = $this->getZoneDetail($zone);
				//print_r( "Name: ".$name.", Zone: ".$zone.", ID: ".$id.", From: ".$fromDate."<br/>");	
				$item=array("id"=>$id,"area"=>$zone,"time"=>$fromDate,"indicator"=>$name,"value"=>$value,"p5"=>$this->unit);
				array_push($items, $item);
							
			}				
			//print_r($items);
			return $items;
		}	

		
		public function listAllSociety(){
			$query = "select * from I_INDEX where ( Name = '".$this->society[0]."' or Name = '".$this->society[1]."' or Name = '".$this->society[2]."' or Name = '".$this->society[3]."') and Zone >='".$this->rangeZone[0]."' and Zone <= '".$this->rangeZone[1]."'";
			$stmt = $this->conn->query( $query ); 
			$items = array();
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				$id = $row['ID'];
				$name = $row['Name'];
				$zone = $row['Zone'];
				$fromDate = $row['FromDate'];
				//cut date to year
				$fromDate = $this->getYear($fromDate);
				$value = $row['Value'];
				//getDetailZone
				$zone = $this->getZoneDetail($zone);
				//print_r( "Name: ".$name.", Zone: ".$zone.", ID: ".$id.", From: ".$fromDate."<br/>");	
				$item=array("id"=>$id,"area"=>$zone,"time"=>$fromDate,"indicator"=>$name,"value"=>$value,"p5"=>$this->unit);
				array_push($items, $item);
							
			}				
			//print_r($items);
			return $items;
		}	
			
		public function delete($id){
				$query = "delete from I_INDEX where ID=".$id;
				$stmt = $this->conn->query( $query );
				//$row = $stmt->fetch( PDO::FETCH_ASSOC );
				
			}
			
		public function update($item){
				//$item = array("id"=>"1","area"=>"杨浦区","time"=>"2012","indicator"=>"GDP","value"=>"123456","p5"=>"xxx");
				$id = $item['id'];
				$zone = $item['area'];
				$zone = $this->getZoneByDistrict($zone);
				$fromDate = $item['time'];
				$fromDate = $this->getFromDateByYear($fromDate);
				$toDate = $fromDate + 1;
				$toDate = $this->getToDateByYear($toDate);
				$name = $item['indicator'];
				$value = $item['value'];
				//p5暂时不管
				$query = "update I_INDEX SET Zone ='".$zone."', FromDate = '".$fromDate."', ToDate = '".$toDate."', Name = '".$name."', Value='".$value."' where ID = '".$id."'";
				//$query = "update I_INDEX SET Zone ='".$zone."', Name = '".$name."', Value='".$value."' where ID = '".$id."'";
				//echo $query;
				$stmt = $this->conn->query( $query );
			}

		public function insert($item){
			//$item = array("id"=>"1","area"=>"杨浦区","time"=>"2012","indicator"=>"GDP","value"=>"123456","p5"=>"xxx");
			
			$zone = $item['area'];
			$zone = $this->getZoneByDistrict($zone);
			$fromDate = $item['time'];
			$fromDate = $this->getFromDateByYear($fromDate);
			$toDate = $fromDate + 1;
			$toDate = $this->getToDateByYear($toDate);
			$name = $item['indicator'];
			$value = $item['value'];
			//p5暂时不管
			// $query = "INSERT INTO I_INDEX(Type,Class,Group,Name,FromDate,ToDate,Remark,Rule,
			// Source,Zone,Value,StdValue,WarnValueHigh,WarnValueLow,Unit) 
			 // VALUES(0,0,0,'".$name."','".$fromDate."','".$toDate."','R','R','1',".$zone.",".$value.",1,1,1,'unit')";
			
			$query = "INSERT INTO [dbo].[I_INDEX]([Type],[Class],[Group],[Name],[FromDate],[ToDate],
			[Remark],[Rule],[Source],[Zone],[Value],[StdValue],[WarnValueHigh],[WarnValueLow]
			,[Unit]) VALUES(0,0,0,'".$name."','".$fromDate."','".$toDate."','R','R','1',".$zone.",".$value.",1,1,1,'unit')";
			
			$stmt = $this->conn->query( $query );
		}
				
		public function getById($id){
			$query = "select * from I_INDEX where ID = '".$id."'";
			$stmt = $this->conn->query( $query );
			$row = $stmt->fetch( PDO::FETCH_ASSOC );
			$id = $row['ID'];
			$name = $row['Name'];
			$zone = $row['Zone'];
			$zone = $this->getZoneDetail($zone);
			$fromDate = $row['FromDate'];
			$fromDate = $this->getYear($fromDate);
			$value = $row['Value'];
			$item = array("id"=>$id,"area"=>$zone,"time"=>$fromDate,"indicator"=>$name,"value"=>$value,"p5"=>$this->unit);
			return $item;
		}
	}
?>